package pers.cz.controller;

import pers.cz.menu.MenuService;
import pers.cz.model.*;
import pers.cz.pub.RespBean;
import pers.cz.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: SystemConfigController
 * @author: Cheng Zhi
 * @create: 2022-01-13 21:32
 **/
@RestController
@RequestMapping(path = "/api/systemConfigController")
public class SystemConfigController {

    @Autowired
    SystemConfigService service;

    @Autowired
    MenuService menuService;

    @GetMapping(path = "/getAllRole")
    public List<Role> getAllRole() {

        List<Role> allRole = service.getParentRole();
        List<Role> result = new ArrayList<Role>();
        for (Role role : allRole) {

            if (role.getRoleId() != null) {

                Role childrenRole = getRole(role);
                if (childrenRole != null) {
                    result.add(childrenRole);
                }
            }
        }
        return result;
    }

    /**
     * 采用递归的方式组织角色树
     * @param parentRole
     * @return
     */
    private Role getRole(Role parentRole) {
        List<Role> roles = service.getRoleByParentId(parentRole.getRoleId());
        if (roles != null) {
            parentRole.setChlidren(roles);
            for (Role role : roles) {
                getRole(role);
            }
        }
        return parentRole;
    }

    @PostMapping("/getRoleById")
    public Role getRoleById(@RequestBody Map map) {

        Integer roleId = (Integer) map.get("roleId");
        if (roleId == null) {
            // 如果传入是空，说明该节点是父亲节点，所以构造一个空Role对象返回
            Role role = new Role();
            role.setRoleNameZh("");
            return role;
        }
        return service.getRoleById(roleId);
    }

    @PostMapping(path = "/updateRole")
    public RespBean updateRole(@RequestBody Map map) {

        Integer roleId = (Integer) map.get("roleId");
        String roleName = (String) map.get("roleName");
        String roleNameZh = (String) map.get("roleNameZh");
        String roleDescribe = (String) map.get("roleDescribe");

        Role role = new Role();
        role.setRoleNameZh(roleNameZh);
        role.setRoleName(roleName);
        role.setRoleDescribe(roleDescribe);
        role.setRoleId(roleId);

        Integer integer = service.updateRole(role);
        return RespBean.ok("修改成功");
    }

    @PostMapping(path = "/addRole")
    public RespBean addRole(@RequestBody Map map) {

        Integer roleParentId = (Integer) map.get("roleParentId");
        String roleName = (String) map.get("roleName");
        String roleNameZh = (String) map.get("roleNameZh");
        String roleDescribe = (String) map.get("roleDescribe");

        Role role = new Role();
        role.setRoleNameZh(roleNameZh);
        role.setRoleName(roleName);
        role.setRoleDescribe(roleDescribe);
        role.setRoleParentId(roleParentId);

        Integer integer = service.addRole(role);
        return RespBean.ok("新增角色成功");
    }

    @PostMapping(path = "/deleteRole")
    public RespBean deleteRole(@RequestBody Map map) {
        Integer roleId = (Integer) map.get("roleId");
        Integer count = service.deleteRole(roleId);
        return RespBean.ok("成功删除[" + count + "]条");
    }

    @GetMapping(path = "/getAllMenu")
    public List<Menu> getAllMenu() {

        List<Menu> allMenu = menuService.getAllParentMenu();
        List<Menu> result = new ArrayList<Menu>();
        for (Menu menu : allMenu) {

            if (menu.getMenuId() != null) {

                Menu childrenMenu = getMenu(menu);
                if (childrenMenu != null) {
                    result.add(childrenMenu);
                }
            }
        }
        return result;
    }

    /**
     * 采用递归的方式组织菜单树
     * @param parentMenu
     * @return
     */
    private Menu getMenu(Menu parentMenu) {
        List<Menu> menus = menuService.getMenuByParentId(parentMenu.getMenuId());
        if (menus != null) {
            parentMenu.setChildren(menus);
            for (Menu menu : menus) {
                getMenu(menu);
            }
        }
        return parentMenu;
    }

    @PostMapping(path = "/getAllMenuId")
    public List<Integer> getAllMenuId(@RequestBody Map map) {

        Integer roleId = (Integer) map.get("roleId");
        return service.getAllMenuId(roleId);
    }

    @PostMapping(path = "/addRoleMenu")
    public RespBean addRoleMenu(@RequestBody Map map) {
        Integer menuId = (Integer) map.get("menuId");
        Integer roleId = (Integer) map.get("roleId");
        service.addPower(roleId, menuId);
        return RespBean.ok("为角色" + roleId + "赋权菜单" + menuId + "成功");
    }

    @PostMapping(path = "/deleteRoleMenu")
    public RespBean deleteRoleMenu(@RequestBody Map map) {
        Integer menuId = (Integer) map.get("menuId");
        Integer roleId = (Integer) map.get("roleId");
        service.deletePower(roleId, menuId);

        return RespBean.ok("为角色" + roleId + "取消菜单" + menuId + "权限成功");
    }

    @PostMapping(path = "/queryAllUserDtl")
    public List<PostgirlDtl> queryAllUserDtl() {

        List<PostgirlDtl> postgirls = service.queryAllUserDtl();
        return  postgirls;
    }

    @PostMapping(path = "queryAllUserDtlByPage")
    public List<PostgirlDtl> queryAllUserDtlByPage(@RequestBody Map map) {

        Integer start = (Integer) map.get("start");
        Integer limit = (Integer) map.get("limit");
        List<PostgirlDtl> postgirlDtls =service.queryAllUserDtlByPage(limit, start);
        return postgirlDtls;
    }
    @PostMapping (path = "/addUser")
    public RespBean addUser(@RequestBody Map map) {

        Integer roleId = (Integer) map.get("roleId");

        String realName = (String) map.get("realName");
        String phoneId = (String) map.get("phoneId");
        String email = (String) map.get("email");

        Role role = new Role();
        role.setRoleId(roleId);
        List<Role> roles = new ArrayList<>();
        roles.add(role);

        String userName = (String) map.get("userName");
        Integer userId = (Integer) map.get("userId");
        String password = (String) map.get("password");
        Integer enable = (Integer) map.get("enable");
        Integer locked = (Integer) map.get("locked");

        Postgirl postgirl = new Postgirl();
        postgirl.setUserId(userId);
        postgirl.setUserName(userName);
        postgirl.setPassWord(password); // 这里需要加密
        postgirl.setEnabled(String.valueOf(enable));
        postgirl.setLocked(String.valueOf(locked));

        postgirl.setRealName(realName);
        postgirl.setPhoneId(phoneId);
        postgirl.setEmail(email);

        postgirl.setRoles(roles);

        Integer integer = service.addUser(postgirl);
        return RespBean.ok("添加成功[" + integer +"]");
    }

    @PostMapping (path = "/updateUser")
    public RespBean updateUser(@RequestBody Map map) {

        Integer roleId = (Integer) map.get("roleId");
        String realName = (String) map.get("realName");
        String phoneId = (String) map.get("phoneId");
        String email = (String) map.get("email");

        Role role = new Role();
        role.setRoleId(roleId);
        List<Role> roles = new ArrayList<>();
        roles.add(role);

        String userName = (String) map.get("userName");
        Integer userId = (Integer) map.get("userId");

        Postgirl postgirl = new Postgirl();
        postgirl.setUserId(userId);
        postgirl.setUserName(userName);
        postgirl.setRealName(realName);
        postgirl.setPhoneId(phoneId);
        postgirl.setEmail(email);

        postgirl.setRoles(roles);

        Integer integer = service.updateUser(postgirl);
        return RespBean.ok("修改成功[" + integer +"]条");
    }

    @PostMapping (path = "/deleteUser")
    public Integer deleteUser(@RequestBody Map map) {
        Integer userId = (Integer) map.get("userId");
        return service.deleteUser(userId);
    }

    @PostMapping (path = "/resetPassword")
    public RespBean resetPassword(@RequestBody Map map) {
        Integer userId = (Integer) map.get("userId");
        service.resetPassword(userId);
        return RespBean.ok("重置成功");
    }
    /**
     *
     * 获取用户总数
     * @return
     */
    @GetMapping(path = "/getCount")
    public Integer getCount() {
        return service.getCount();
    }

    /**
     * 修改锁定状态
     * @param map
     */
    @PostMapping(path = "/updateLockedState")
    public void updateLockedState(@RequestBody Map map) {

        Integer userId = (Integer) map.get("userId");
        Integer locked = (Integer) map.get("locked");

        service.updateLockedState(userId, locked);
    }

    /**
     * 修改禁用状态
     * @param map
     */
    @PostMapping(path = "/updateEnableState")
    public void updateEnableState(@RequestBody Map map) {

        Integer userId = (Integer) map.get("userId");
        Integer enable = (Integer) map.get("enable");

        service.updateEnableState(userId, enable);
    }

    /**
     * 获取选定用户的角色信息
     * @param map
     * @return
     */
    @PostMapping(path = "/getCurrentRoleByUserId")
    public Integer getCurrentRoleByUserId(@RequestBody Map map) {

        Integer userId = (Integer) map.get("userId");
        return service.getCurrentRoleByUserId(userId);
    }

    /**
     * 获取选定用户的明细信息
     * @param map
     * @return
     */
    @PostMapping(path = "/getCurrentUserDtlByUserId")
    public PostgirlDtl getCurrentUserDtlByUserId(@RequestBody Map map) {
        Integer userId = (Integer) map.get("userId");
        return service.getCurrentUserDtlByUserId(userId);
    }

    /**
     * 获取指定类别的变量
     * @param map
     * @return
     */
    @PostMapping(path = "/getSystemVarCfgByType")
    public List<SystemVarCfg> getSystemVarCfgByType(@RequestBody Map map) {
        String varType = (String) map.get("varType");
        return service.getSystemVarCfg(varType);
    }

}
